package longrise.phone.com.bjjt_jyb.Utils;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import com.longrise.LEAP.Base.Objects.EntityBean;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;

import longrise.phone.com.bjjt_jyb.Application.BaseApplication;
import longrise.phone.com.bjjt_jyb.R;


/**
 * 图形验证码帮助类
 */
public class VerifityHelper {

    private Context mContext;

    private View view;

    private ImageView picIV;

    private EditText codeET;

    private DisplayImageOptions imageOptions;

    private BaseApplication baseApplication;

    private EntityBean picBean;//图片验证码的参数

    public VerifityHelper(View view, Context context) {
        this.view = view;
        this.mContext = context;

        initViews();
    }

    /**
     * 验证是否参数是否正确
     *
     * @return
     */
    public boolean checkPicCode() {
        if (null == picBean) {
            UiUtil.showToast(mContext, "请获取验证码图片");
            return false;
        }
        String code = codeET.getText().toString().trim();
        if (TextUtils.isEmpty(code)) {
            UiUtil.showToast(mContext, "请输入正确的图片验证码");
            return false;
        }
        return true;
    }

    public EntityBean getPicBean() {
        return picBean;
    }

    public String getImgCode() {
        return codeET.getText().toString().trim();
    }

    private void initViews() {
        if (view == null) {
            return;
        }
        picIV = (ImageView) view.findViewById(R.id.iv_getvalidcode);
        codeET = (EditText) view.findViewById(R.id.et_getvalidcode);
        picIV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                loadPicCode();
            }
        });

        imageOptions = new DisplayImageOptions.Builder()
                .showStubImage(R.drawable.loadcode) // 设置图片下载期间显示的图片
                .showImageForEmptyUri(R.drawable.faletogetcode) // 设置图片Uri为空或是错误的时候显示的图片
                .showImageOnFail(R.drawable.faletogetcode) // 设置图片加载或解码过程中发生错误显示的图片
                .imageScaleType(ImageScaleType.NONE) // 图片不会调整
                .cacheInMemory(true) // 设置下载的图片是否缓存在内存中
                .cacheOnDisc(true) // 设置下载的图片是否缓存在SD卡中
                .build();
        baseApplication = (BaseApplication) mContext.getApplicationContext();
    }


    /**
     * 加载图片验证码
     */
    public void loadPicCode() {
        ApiImpI.getInstance(baseApplication).picCodeCreate(new ApiImpI.RequestCallBack() {
            @Override
            public void onError(String error) {
                if (!TextUtils.isEmpty(error)) {
                    UiUtil.showToast(mContext, error);
                }
            }

            @Override
            public void onSuccess(Object result) {
                EntityBean bean = (EntityBean) result;
                String restate = bean.getString("restate");
                picBean = null;//重置参数
                if (TextUtils.equals(restate, "1")) {
                    picBean = bean.getBean("data");
                } else {
                    String reses = bean.getString("redes");
                    UiUtil.showToast(mContext, reses);
                }
                showCodePic();
            }
        });
    }

    /**
     * 显示图片验证码的加载结果
     */
    private void showCodePic() {
        if (picBean == null) {
            picIV.setImageResource(R.drawable.faletogetcode);
        } else {
            String url = picBean.getString("img");
            ImageLoader.getInstance().displayImage(url, picIV, imageOptions);// 加载图片
        }
    }
}
